//
//  BrandSeriesViewController.m
//  AppointmentForCar
//
//  Created by Herron on 2017/1/17.
//  Copyright © 2017年 Herron. All rights reserved.
//

#import "BrandSeriesViewController.h"
#import "SeriesModel.h"
#import "ModifyUserInfoViewController.h"
#import "UserInfoViewController.h"

@interface BrandSeriesViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *tableArray;

@end

@implementation BrandSeriesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"型号";
    self.tableArray = @[].mutableCopy;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CarBrandListCellID"];
    
    [HUDManager showLoadingHUDViewWithText:@"正在加载"];
    [NetWork postNetWorkWithUrl:kURLBrandSeriesList parame:@{@"brandId":self.brandId} successBlock:^(NSDictionary *dic) {
        [HUDManager hideHUDView];
        NSLog(@"车辆型号 = %@", dic);
        NSArray *brandList = dic[@"data"][@"seriesList"];
        if (brandList.count) {
            self.tableArray = [SeriesModel arrayOfModelsFromDictionaries:brandList error:nil];
            [self.tableView reloadData];
        } else {
            [HUDManager showWarningWithText:@"暂无数据"];
        }
    } errorBlock:^(id error) {
        [HUDManager showWarningWithText:@"网络异常"];
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - <UITableViewDataSource, UITableViewDelegate>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.tableArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SeriesModel *model = self.tableArray[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CarBrandListCellID"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = model.name;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([_type isEqualToString:@"1"]) {
        for (UIViewController *vc in self.navigationController.viewControllers) {
            if ([vc isKindOfClass:[UserInfoViewController class]]) {
                SeriesModel *model = self.tableArray[indexPath.row];
                model.name = [NSString stringWithFormat:@"%@ %@", _brandName, model.name];
                NSDictionary *dic = @{@"brand":model};
                [[NSNotificationCenter defaultCenter] postNotificationName:@"sendBrandForRegister" object:nil userInfo:dic];
                [self.navigationController popToViewController:vc animated:YES];
            }
        }
    } else {
        for (UIViewController *vc in self.navigationController.viewControllers) {
            if ([vc isKindOfClass:[ModifyUserInfoViewController class]]) {
                SeriesModel *model = self.tableArray[indexPath.row];
                model.name = [NSString stringWithFormat:@"%@ %@", _brandName, model.name];
                NSDictionary *dic = @{@"brand":model};
                [[NSNotificationCenter defaultCenter] postNotificationName:@"sendBrand" object:nil userInfo:dic];
                [self.navigationController popToViewController:vc animated:YES];
            }
        }
    }
}



@end
